package com.lc.sell.controller;


import com.lc.sell.dto.OrderDTO;
import com.lc.sell.service.WebSocket;
import com.lc.sell.service.serviceImpl.OrderServiceImpl;
import com.lc.sell.util.IdGenerator;
import com.lc.sell.vo.OrderPay;
import com.lc.sell.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 龙晨
 * @since 2022-02-01
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private WebSocket webSocket;
    @Autowired
    private OrderServiceImpl orderService;

    @GetMapping
    public ModelAndView toOrder(HttpServletRequest request){
        Integer mid = (Integer) request.getSession().getAttribute("mid");
        ModelAndView mv = new ModelAndView();
        mv.setViewName("page/jiedan_list");
        mv.addObject("list",orderService.findAllNewOrder(mid));
        return mv;
    }
    
    @GetMapping("orderList")
    public ModelAndView toOrderList(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("page/order_list");
        webSocket.sendMessage("跳转消息");
        return mv;
    }

    @ApiOperation(value = "创建新的订单")
    @PostMapping("createOrder")
    public ResultVo createOrder(@RequestBody OrderDTO orderDTO){
        return orderService.createOrder(orderDTO);
    }

    @ApiOperation(value = "修改订单状态订单")
    @PutMapping("updateOrderStatus")
    public ResultVo updateOrderStatus(@RequestParam String oid){
        return orderService.updateOrderStatus(oid);
    }

    @ApiOperation(value = "获取商家订单")
    @GetMapping("orderLists")
    public ResultVo orderList(@RequestParam Integer mid){
        return orderService.orderList(mid);
    }

    @ApiOperation(value = "用户订单")
    @GetMapping("userOrderList")
    public ResultVo userOrderList(@RequestParam Integer uid){
        return orderService.userOrderList(uid);
    }

    @ApiOperation(value = "查询订单状态")
    @GetMapping("selectOrderStatus")
    public ResultVo selectOrderStatus(@RequestParam String orderId){
        return orderService.selectOrderStatus(orderId);
    }

    @ApiOperation(value = "修改订单评论状态")
    @GetMapping("updateOrderReview")
    public ResultVo updateOrderReview(@RequestParam String orderId,@RequestParam Integer review){
        return orderService.updateOrderReview(orderId,review);
    }

    /**
     * alipay.trade.wap.pay：H5手机网站支付接口2.0（外部商户创建订单并支付）
     * @param orderPay
     * @return
     */
    @RequestMapping(value="/alipayTradeWapPay")
    @ResponseBody
    public ResultVo alipayTradeWapPay(@RequestBody OrderPay orderPay) {
        return orderService.alipayTradeWapPay(orderPay);
    }
}
